package fc.testmain;


import cn.hutool.core.util.CharsetUtil;
import cn.hutool.crypto.asymmetric.KeyType;
import cn.hutool.crypto.asymmetric.RSA;
import fc.FcHttpCon;
import fc.GetToken;
import fc.entity.OaEntity;
import weaver.upgradetool.httpclient.org.apache.http.client.HttpClient;
import weaver.upgradetool.httpclient.org.apache.http.client.entity.UrlEncodedFormEntity;
import weaver.upgradetool.httpclient.org.apache.http.client.methods.HttpPost;
import weaver.upgradetool.httpclient.org.apache.http.impl.client.DefaultHttpClient;
import weaver.upgradetool.httpcore.org.apache.http.HttpResponse;
import weaver.upgradetool.httpcore.org.apache.http.NameValuePair;
import weaver.upgradetool.httpcore.org.apache.http.message.BasicNameValuePair;
import weaver.upgradetool.httpcore.org.apache.http.util.EntityUtils;

import java.util.ArrayList;
import java.util.List;


public class CreateRequestinfoTEST111 {

    public static void diaoyong(List<OaEntity> dt1){
        GetToken tok=new GetToken();
        String token=(String) tok.testGetoken().get("token");

        RSA rsa = new RSA(null, FcHttpCon.SPK);
        String encryptUserid = rsa.encryptBase64(dt1.get(0).getUSERID(),CharsetUtil.CHARSET_UTF_8,KeyType.PublicKey);
        // 1. 创建HttpClient对象
        HttpClient httpClient = new DefaultHttpClient();

        // 2. 创建HttpPost对象，并设置请求的URL
        HttpPost httpPost = new HttpPost("http://oa.maxwell-gp.com:8090/api/workflow/paService/doCreateRequest");
        try {
            String maindata="["+
                    "{\"fieldName\":\"djrq\",\"fieldValue\":\""+dt1.get(0).getWLXQSJ()+"\"},"+
                    "{\"fieldName\":\"applydept\",\"fieldValue\":\""+dt1.get(0).getDEPARTMENTID()+"\"},"+
                    //"{\"fieldName\":\"t100ry\",\"fieldValue\":\""+dt1.get(0).getUSERID()+"\"},"+
                    "{\"fieldName\":\"gys\",\"fieldValue\":\""+dt1.get(0).getCUS_NO()+"\"},"+
                    "{\"fieldName\":\"sxrq\",\"fieldValue\":\""+dt1.get(0).getSXRQ()+"\"},";

            if(null!=dt1.get(0).getJZRQ()){
                maindata+="{\"fieldName\":\"jzrq\",\"fieldValue\":\""+dt1.get(0).getJZRQ()+"\"},";
            }

            maindata+=
                    "{\"fieldName\":\"hjdb\",\"fieldValue\":\""+dt1.get(0).getTYPENO()+"\"},"+
                            "]";
            System.out.println(maindata);
            String detailData="[";
            detailData+="{\"tableDBName\":\"formtable_main_722_dt1\",\"workflowRequestTableRecords\":[";
            for (int i=0;i<dt1.size();i++){
                detailData+=
                        // 每一行开始
                        "{\"recordOrder\":0,\"workflowRequestTableFields\":["
                                + "{\"fieldName\":\"lh\",\"fieldValue\":\""+dt1.get(i).getLJBH()+"\"},"//料号
                                + "{\"fieldName\":\"dj\",\"fieldValue\":\""+dt1.get(i).getUP()+"\"},"//单价
                                + "{\"fieldName\":\"bz\",\"fieldValue\":\""+dt1.get(i).getREM()+"\"},"//备注
                                + "{\"fieldName\":\"zjyy\",\"fieldValue\":\""+dt1.get(i).getCAUSE()+"\"}"+//涨价原因
                                "]},";

                // 每一行结束
            }

            detailData+="]}";
            detailData+="]";

            System.out.println(detailData);

            // 3. 创建参数列表，并进行参数编码
            List<NameValuePair> params = new ArrayList<>();
            params.add(new BasicNameValuePair("workflowId", dt1.get(0).getJUDGE3()));
            params.add(new BasicNameValuePair("requestName", "采购核价单-"+dt1.get(0).getJUDGE2()+"-"+dt1.get(0).getWLXQSJ()));
            params.add(new BasicNameValuePair("mainData",maindata));
            params.add(new BasicNameValuePair("detailData",detailData));

            params.add(new BasicNameValuePair("otherParams","{\"isnextflow\":\"0\"}"));
            UrlEncodedFormEntity entity = new UrlEncodedFormEntity(params, "UTF-8");

            // 4. 将参数设置到HttpPost对象的Entity中
            httpPost.setEntity(entity);
            httpPost.setHeader("appid",FcHttpCon.APPID);
            httpPost.setHeader("token",token);
            httpPost.setHeader("userid",encryptUserid);

            // 5. 发送请求，并获取响应结果
            HttpResponse response = httpClient.execute(httpPost);
            String responseBody = EntityUtils.toString(response.getEntity());
            System.out.println(responseBody);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {



        //供应商	CUS_NO 生效日期SXRQ	失效日期JZRQ 	核价单别TYPENO	税种TAX_NO	付款条件FKTJ	币种CUR_ID	交易条件JYTJ	料号LJBH	单价UP	备注REM	涨价原因CAUSE
        // hbxh judge	单据日期WLXQSJ 	申请部门DEPARTMENTID	申请人USERID
        List<OaEntity> dt1=new ArrayList<OaEntity>();OaEntity oa1=new OaEntity();
        oa1.setCUS_NO("S11181");
        oa1.setSXRQ("2025-06-16");
        oa1.setJZRQ(null);
        oa1.setTYPENO(null);
        oa1.setTAX_NO("C04");
        oa1.setFKTJ("A94");
        oa1.setCUR_ID("AWG");
        oa1.setJYTJ("FCA");
        oa1.setLJBH("120103000113");
        oa1.setUP("100");
        oa1.setREM("备注");
        oa1.setCAUSE("CAUSE");
        oa1.setJUDGE1("1");
        oa1.setWLXQSJ("2025-06-16");
        oa1.setDEPARTMENTID("1000055");
        oa1.setUSERID("243");
        oa1.setJUDGE2("费诚");//复用姓名
        oa1.setJUDGE3("2294");//workflowid
        oa1.setJUDGE4("XH1");
        dt1.add(oa1);

        OaEntity oa2=new OaEntity();
        oa2.setCUS_NO("S11181");
        oa2.setSXRQ("2025-06-16");
        oa2.setJZRQ(null);
        oa2.setTYPENO(null);
        oa2.setTAX_NO("C04");
        oa2.setFKTJ("A94");
        oa2.setCUR_ID("AWG");
        oa2.setJYTJ("FCA");
        oa2.setLJBH("120103000113");
        oa2.setUP("100");
        oa2.setREM("备注");
        oa2.setCAUSE("CAUSE");
        oa2.setJUDGE1("1");
        oa2.setWLXQSJ("2025-06-16");
        oa2.setDEPARTMENTID("1000055");
        oa2.setUSERID("243");
        oa2.setJUDGE2("费诚");//复用姓名
        oa2.setJUDGE3("2294");//workflowid
        oa2.setJUDGE4("XH1");
        dt1.add(oa2);

        OaEntity oa3=new OaEntity();
        oa3.setCUS_NO("S11181");
        oa3.setSXRQ("2025-06-16");
        oa3.setJZRQ(null);
        oa3.setTYPENO(null);
        oa3.setTAX_NO("C04");
        oa3.setFKTJ("A94");
        oa3.setCUR_ID("AWG");
        oa3.setJYTJ("FCA");
        oa3.setLJBH("120103000113");
        oa3.setUP("100");
        oa3.setREM("备注");
        oa3.setCAUSE("CAUSE");
        oa3.setJUDGE1("1");
        oa3.setWLXQSJ("2025-06-16");
        oa3.setDEPARTMENTID("1000055");
        oa3.setUSERID("243");
        oa3.setJUDGE2("费诚");//复用姓名
        oa3.setJUDGE3("2294");//workflowid
        oa3.setJUDGE4("XH11");
        dt1.add(oa3);

        OaEntity oa4=new OaEntity();
        oa4.setCUS_NO("S11181");
        oa4.setSXRQ("2025-06-16");
        oa4.setJZRQ(null);
        oa4.setTYPENO(null);
        oa4.setTAX_NO("C04");
        oa4.setFKTJ("A94");
        oa4.setCUR_ID("AWG");
        oa4.setJYTJ("FCA");
        oa4.setLJBH("120103000113"); //dt
        oa4.setUP("100");
        oa4.setREM("备注");
        oa4.setCAUSE("CAUSE");
        oa4.setJUDGE1("1");
        oa4.setWLXQSJ("2025-06-16");
        oa4.setDEPARTMENTID("1000055");
        oa4.setUSERID("243");
        oa4.setJUDGE2("费诚");//复用姓名
        oa4.setJUDGE3("2294");//workflowid
        oa4.setJUDGE4("XH11");
        dt1.add(oa4);


        String judge_add="";
        List<OaEntity> dts=new ArrayList<OaEntity>();

        for (int ii=0;ii<dt1.size();ii++){
             if(judge_add.equals(dt1.get(ii).getJUDGE4())||"".equals(judge_add)){
                dts.add(dt1.get(ii));
            }else{
                //调用接口生成
                diaoyong(dts);
                dts=new ArrayList<OaEntity>();
                dts.add(dt1.get(ii));
            }
            judge_add=dt1.get(ii).getJUDGE4();
         }
        if(dts.size()>0){
            //调用接口生成
            diaoyong(dts);
        }


    }



}
